﻿using System;
using System.Diagnostics;

namespace CSharp.Libs.ExceptionHandling
{
    public class ExceptionTools
    {
        /// <summary>m_GetCurrentMethodName
        ///     gets the name of the calling method.
        /// </summary>
        /// <returns>
        ///     Name of the method that called m_GetCurrentMethodName
        /// </returns>
        /// <remarks>
        ///     Used mainly by exception handling to supply handler with the name of the 
        ///     method where exception occured.
        /// </remarks>
        /// <example>
        ///     <c>DoWork</c> method calls m_GetCurrentMethodName in its catch block,
        ///     receives the string "DoWork" and gives it to the <c>AddException</c> method of the
        ///     exception handler so that he knows what method threw the exception.
        ///     <c>
        ///     catch (Exception Ex)
        ///     {
        ///         m_KRNL.EC.AddException(Ex, this.ToString(), m_GetCurrentMethodName());
        ///     }
        ///     </c>
        ///     That assures that the handler always has the correct method name. Even if the name 
        ///     is changed, the coder doesn't need to chenge it in the catch block also.
        /// </example>
        /// <history>created 28.05.2009 VB</history>
        /// <history>tested 28.05.2009 VB</history>
        public String GetCurrentMethodName()
        {
            StackFrame stackframe = new StackFrame(1, true);
            return stackframe.GetMethod().ToString();
        }
    }
}
